package com.island.gomallorder.pay;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.island.gomallorder.config.AlipayProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author MarkTien
 * @Mail marktien@foxmail.com
 **/
@Slf4j
@Component
public class GomallAlipayClient {

    @Resource
    private AlipayProperty alipayProperty;

    public boolean commitOrder(Map<String,Object> commitContent) {
        try{
            AlipayClient alipayClient = new DefaultAlipayClient(alipayProperty.getURL(),alipayProperty.getAPPID(),alipayProperty.getRSA_PRIVATE_KEY(),"json",alipayProperty.getCHARSET(),alipayProperty.getALIPAY_PUBLIC_KEY(),alipayProperty.getSIGNTYPE());
            AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
            JSONObject bizContent = new JSONObject();
            bizContent.putAll(commitContent);
            request.setBizContent(bizContent.toString());
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            return response.isSuccess();
        }catch (AlipayApiException e){
            log.error("alipay远程调用异常",e);
            return false;
        }
    }
}
